<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //过滤空白字符串
        // /^\s*|\s*$/g
        function abc(){
            var oldstr=document.getElementById('old');
            var newstr=document.getElementById('new');
            var reg=/^\s*|\s*$/g;
            newstr.value=oldstr.value.replace(reg,'')
        }
    </script>
</head>

<body>
   过滤前:<input type="text" id="old">
   <button onclick="abc()">过滤空格<tton><br>
   过滤后:<input type="text" id="new">
   
</body>

</html>
